﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace CustomControls
{
    public partial class XTabControl : UserControl
    {
        Panel userPanel;
        XButtonPanel tabPanel;

        public XTabControl()
        {
            tabPanel = new XButtonPanel(XButton.ButtonBehavior.Radio, new Font(FontFamily.GenericSansSerif, 9.0f, FontStyle.Regular));
            userPanel = new Panel();
            userPanel.BackColor = Color.Red;

            tabPanel.SelectedItemChanged += new XButtonPanel.SelectedItemChangedHandler(tabPanel_SelectedItemChanged);
            
            this.Controls.Add(tabPanel);
            this.Controls.Add(userPanel);
        }

        public bool SelectItem(int index)
        {
            return this.tabPanel.SelectItem(index);
        }

        void tabPanel_SelectedItemChanged(object sender, int index)
        {
            for (int i = 0; i < this.userPanel.Controls.Count; i++)
            {
                if (i == index)
                {
                    this.userPanel.Controls[i].Visible = true;
                }
                else
                {
                    this.userPanel.Controls[i].Visible = false;
                }
            }
        }

        public void Add(Control control)
        {
            control.Dock = DockStyle.Fill;
            userPanel.Controls.Add(control);
            tabPanel.AddButton(new XButton(new Size(150, this.ClientSize.Height), control.Name, Color.Black));
        }

        protected override void OnResize(EventArgs e)
        {
            base.OnResize(e);

            if ((double)this.ClientSize.Width / (double)this.ClientSize.Height < 1)
            {
                tabPanel.Dock = DockStyle.None;
                userPanel.Height = this.ClientSize.Height - tabPanel.Height;
                tabPanel.Dock = DockStyle.Bottom;
                tabPanel.Height = 80;
            }
            else
            {
                tabPanel.Dock = DockStyle.None;
                userPanel.Width = this.ClientSize.Width - tabPanel.Width;
                tabPanel.Dock = DockStyle.Right;
                tabPanel.Width = 100;
            }

            userPanel.Dock = DockStyle.Fill;
            userPanel.BringToFront();
        }
    }
}
